<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php'); 
require('../Excel/Excel/reader.php');
class GuideExcel
{
	var $guide_excel_c;
	var $upFilePath = "/var/www/website/cms.wondershare.com.br/httpdocs/Excel/xls/";
	/**
	* @ 构造函数,实例化adControl
	* @ __construct
	*/
	public function __construct()
	{
	//		$this->guide_excel_c = new GuideExcelControl();
	}
	function deletehtml($str)  
	{  
		$str = trim($str);  
		$str=preg_replace("{\t}","",$str);  
		$str=preg_replace("{\r\n}","",$str);  
		$str=preg_replace("{\r}","",$str);  
		$str=preg_replace("{\n}","",$str);  
		return $str;  
	}  
	/**
	* @函数export_excel,导入Excel
	* @param  NULL
	* @return  string
	*/
	function export_excel()
	{
		$field_list_checkbox = "";
		if($_POST['sub_from'])
		{
			$filename = trim($_POST['file_name']);//文件名
			$field_list = trim($_POST['field_list']);//字段列表
			$web_type = trim($_POST['web_type']);//网站类型
			$file_type = "vnd.ms-excel"; 
			header("Content-Type: application/$file_type"); 
			header("Content-Disposition: attachment; filename=$filename"); 
			header("Pragma: no-cache"); 
			header("Expires: 0"); 
			$field_rows = explode(',',$field_list);
			foreach ($field_rows AS $key=>$values)
			{
				echo  $values."\t";
			}
			echo "\n";
			$sql = 'SELECT  '.$field_list.'  FROM  topic_article  WHERE web_type = '.$web_type ; 
			
			$result = $GLOBALS['db']->query($sql);
			while ($rows = $GLOBALS['db']->fetch_array($result))
			{
				foreach ($field_rows AS $key => $values)
				{
				    echo iconv('utf-8', 'gbk', $this->deletehtml(trim($rows[trim($values)])))."\t";
				}
				echo "\n";
			}
			exit;
		
		}
		$sql = "SELECT * FROM  topic_article  ";
		$result = $GLOBALS['db']->query($sql);
		while ($field_rows = mysql_fetch_field($result))
		{
			$field_list_checkbox .= '<input type="checkbox" name="field_list[]"  
			id="field_'.$field_rows->name.'" '.$check_str.' value="'.$field_rows->name.'"  >'.$field_rows->name.'<br>';
		
		}
		$GLOBALS['smarty']->assign('field_list_checkbox',  $field_list_checkbox);
		$GLOBALS['smarty']->display('guide_export.htm'); 
	}
	/**
	* @函数import,导入Excel
	* @param  NULL
	* @return  string
	*/
	function import_excel()
	{
		$tables_data = array(
		'ws'=>array('w_type'=>1,'tem_table'=>'Table_13','tem_id'=>'Field_19965','tem_name'=>'Field_19978',
		'tem_title' => 'Field_19964','tem_keywords' => 'Field_19914','tem_description' => 'Field_19915',
		'tem_path'=>'/wondershare.com/httpdocs','web_name' =>'http://www.wondershare.com'),
		
		'am' =>array('w_type'=>2,'tem_table'=>'Table_59','tem_id'=>'Field_20531','tem_name'=>'Field_20532',
		'tem_title' => 'Field_20533','tem_keywords' => 'Field_20534','tem_description' => 'Field_20535',
		'tem_path'=>'/aimersoft.com/httpdocs','web_name' =>'http://www.aimersoft.com'),
		
		'any' =>array('w_type'=>3,'tem_table'=>'Table_55','tem_id'=>'Field_20486','tem_name'=>'Field_20487',
		'tem_title' => 'Field_20483','tem_keywords' => 'Field_20484','tem_description' => 'Field_20485',
		'tem_path'=>'/anypdftools.com/httpdocs','web_name' =>'http://www.anypdftools.com'),
		
		'app' =>array('w_type'=>4,'tem_table'=>'Table_51','tem_id'=>'Field_20428','tem_name'=>'Field_20429',
		'tem_title' => 'Field_20430','tem_keywords' => 'Field_20431','tem_description' => 'Field_20432',
		'tem_path'=>'/applemacvideo.com//httpdocs','web_name' =>'http://www.applemacvideo.com'),
		
		'jp' =>array('w_type'=>5,'tem_table'=>'Table_66','tem_id'=>'Field_20658','tem_name'=>'Field_20659',
		'tem_title' => 'Field_20660','tem_keywords' => 'Field_20661','tem_description' => 'Field_20662',
		'tem_path'=>'/wondershare.jp/httpdocs','web_name' =>'http://www.wondershare.jp'),
		
		'de' =>array('w_type'=>6,'tem_table'=>'Table_62','tem_id'=>'Field_20573','tem_name'=>'Field_20574',
		'tem_title' => 'Field_20575','tem_keywords' => 'Field_20576','tem_description' => 'Field_20577',
		'tem_path'=>'/wondershare.de/httpdocs','web_name' =>'http://www.wondershare.de'),
		
		'sp' =>array('w_type'=>7,'tem_table'=>'Table_47','tem_id'=>'Field_20382','tem_name'=>'Field_20383',
		'tem_title' => 'Field_20377','tem_keywords' => 'Field_20378','tem_description' => 'Field_20379',
		'tem_path'=>'/spotmau.com/httpdocs','web_name' =>'http://www.spotmau.com'),
		
		'is' =>array('w_type'=>8,'tem_table'=>'Table_71','tem_id'=>'Field_20681','tem_name'=>'Field_20682',
		'tem_title' => 'Field_20483','tem_keywords' => 'Field_20484','tem_description' => 'Field_20485',
		'tem_path'=>'/iskysoft.com/httpdocs','web_name' =>'http://www.iskysoft.com'),
		
		'fr' =>array('w_type'=>11,'tem_table'=>'Table_102','tem_id'=>'Field_21303','tem_name'=>'Field_21304',
		'tem_title' => 'Field_21305','tem_keywords' => 'Field_21306','tem_description' => 'Field_21307',
		'tem_path'=>'/wondershare.fr/httpdocs','web_name' =>'http://www.wondershare.fr'),
		
		'es' =>array('w_type'=>12,'tem_table'=>'Table_113','tem_id'=>'Field_21390','tem_name'=>'Field_21391',
		'tem_title' => 'Field_21392','tem_keywords' => 'Field_21393','tem_description' => 'Field_21394',
		'tem_path'=>'/wondershare.com.br/httpdocs','web_name' =>'http://www.wondershare.com.br'),
		
		'mx' =>array('w_type'=>13,'tem_table'=>'Table_106','tem_id'=>'Field_21322','tem_name'=>'Field_21323',
		'tem_title' => 'Field_21324','tem_keywords' => 'Field_21325','tem_description' => 'Field_21326',
		'tem_path'=>'/wondershare.com.mx/httpdocs','web_name' =>'http://www.wondershare.com.mx'),
		
		'br' =>array('w_type'=>14,'tem_table'=>'Table_114','tem_id'=>'Field_21443','tem_name'=>'Field_21444',
		'tem_title' => 'Field_21445','tem_keywords' => 'Field_21446','tem_description' => 'Field_21447',
		'tem_path'=>'/wondershare.com.br/httpdocs','web_name' =>'http://www.wondershare.com.br'));
		if($_POST['sub_from'])
		{
			$new_data = array();
			$file_name = $_POST['filename'];//得到文件名
			
			$web_name = $_POST['web_name'];//得到网站类型
			$data = new Spreadsheet_Excel_Reader();
			//设置文本输出编码
			$data->setOutputEncoding('utf8');
			//读取Excel文件
			$data->read("/var/www/html/cms.com/Excel/xls/".$file_name);
			//$data->sheets[0]['numCols']为Excel列数
			if($data->sheets[0]['cells'][1][1] != 'ar_page_title')
			{
				$msg = '正规模板第一列应该是文章页面标题！';
			}
			elseif($data->sheets[0]['cells'][1][2] != 'ar_page_keyword')
			{
				$msg = '正规模板第一列应该是文章页面关键字！';
			}
			elseif($data->sheets[0]['cells'][1][3] != 'ar_page_description')
			{
				$msg = '正规模板第一列应该是文章页面描述！';
			}
			elseif($data->sheets[0]['cells'][1][4] != 'ar_content')
			{
				$msg = '正规模板第一列应该是文章内容！';
			}
			elseif($data->sheets[0]['cells'][1][5] != 'ar_title')
			{
				$msg = '正规模板第一列应该是文章标题！';
			}
			elseif($data->sheets[0]['cells'][1][6] != 'ar_subtitle')
			{
				$msg = '正规模板第一列应该是文章短标题！';
			}
			elseif($data->sheets[0]['cells'][1][7] != 'ar_url')
			{
				$msg = '正规模板第一列应该是文章URL！';
			}
			elseif(!empty($data->sheets[0]['cells'][1][8]))
			{
				$msg = '正规模板第八列应该木有啦！';
			}
			else
			{
				//$data->sheets[0]['numRows']为Excel行数
				for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) 
				{
					//$data->sheets[0]['numCols']为Excel列数
					if(!empty($data->sheets[0]['cells'][$i]))
					{
					  $new_data[] = $data->sheets[0]['cells'][$i];
					}
				
				}
				foreach($new_data as $val)
				{
					if(!empty($val))
					{
					    $this->insert_base_tem($val,$tables_data[$web_name]);//向基础表和模板表插入数据即导数据
					}
				}
				$msg = '导入成功！';
			}
			echo  '<script type="text/javascript"> 
			alert("'.$msg.'");
			</script>';
		
		}
		$GLOBALS['smarty']->display('guide_import.htm'); 
	
	}
	/**
	* @函数file_upload文件上传
	* @param  NULL
	* @return  string
	*/
	public function file_upload()
	{
		$filepath = $this->upFilePath.$_FILES['excel_file']['name'];
		if(file_exists($filepath))
		{
			$msg =  "该文件已经存在,请重新选择文件，或者重新命名！";
			$error = 1;
		}
		else
		{
			$upload_ok = @move_uploaded_file($_FILES['excel_file']['tmp_name'],$filepath);
			if($upload_ok === FALSE)
			{
				$msg =   '上传失败！';
				$error = 1;
			}else
			{
				$msg =  '上传成功！';
				$fileName = $_FILES['excel_file']['name'];
				$error = 0;
			}   
		}
		echo "{";
		echo				"error: '" . $error . "',\n";
		echo				"msg: '" . $msg . "',\n";
		echo				"filename : '" . $fileName  . "'\n";
		echo "}";
	}
	/**
	* @函数download下载
	* @param  NULL
	* @return  string
	*/
	public function download()
	{
		$path = $this->upFilePath.'test.xls';
		header('Content-Type:application/octet-stream'); 
		header('accept-length:'.filesize($path));
		header('Content-Disposition:attachment;filename='.basename($path));
		readfile($path);
	
	}

	/**
	* @函数insert_base_tem导入到模板
	* @param  NULL
	* @return  string
	*/
	public function insert_base_tem($val,$tables)
	{
		$sql = "SELECT COUNT(*) AS nums FROM topic_article WHERE ar_title = '".addslashes($val[5])."' AND  web_type = ".$tables['w_type'];
		$nums = $GLOBALS['db']->getOne($sql);
		if($nums == 0)//判断标题是否存在
		{
			$sql = "INSERT INTO topic_article(ar_title,ar_subtitle,ar_introduction,ar_content,ar_image,ar_vicecat_id,
			ar_cat_id,ar_tag_id,ar_page_title,ar_page_keyword,ar_page_description,add_time,add_user,update_time,
			update_user,pub_time,is_recommend,state,click_count,is_hot,is_delete,pub_status,related_article,
			modify_status,template_url,web_type) VALUES ('".addslashes($val[5])."','".addslashes($val[6])."',
			'".addslashes($val[3])."','".addslashes($val[4])."','','','0','','".addslashes($val[1])."',
			'".addslashes($val[2])."','".addslashes($val[3])."',UNIX_TIMESTAMP(),'administrator',UNIX_TIMESTAMP(),
			'administrator','0','0','0','0','0','0','0','','0','',".$tables['w_type'].")";
			if($GLOBALS['db']->query($sql)) 
			{
			   $ar_id = $GLOBALS['db']->insert_id();
			   $url = trim(str_replace($tables['web_name'],$tables['tem_path'],$val[7]));
			   if($ar_id) 
			   {
			      $GLOBALS['db']->query("INSERT INTO ".$tables['tem_table']." (`url`,`CreatorID`,`MenderID`,`CreateTime`,`MendTime`,`page_num`,
			      ".$tables['tem_id'].",".$tables['tem_title'].",".$tables['tem_keywords'].",".$tables['tem_description'].",".$tables['tem_name']."
			      )values('".$url."','1','1',UNIX_TIMESTAMP(),UNIX_TIMESTAMP(),'1','{$ar_id}','".addslashes($val[1])."',
			      '".addslashes($val[2])."','".addslashes($val[3])."','".addslashes($val[5])."')");
			   }
			}
		}
	}

}
$action    = $_REQUEST['action']; 
$guide_excel  = new GuideExcel;
if(method_exists($guide_excel, $action)) 
{
	$guide_excel->$action();
}else{
	$guide_excel->index();
}

?>
